package cxy.highConcurrency;

public class ThreadLocalDemo_2 {
	static ThreadLocal<String> threadLocal = new ThreadLocal<>();
	
	public static void main(String[] args) {
		new Thread(() -> {
			try {
				threadLocal.set(Thread.currentThread().getName());
				System.out.println(Thread.currentThread().getName() + "," + threadLocal.get());
			} finally {
				threadLocal.remove();
			}
		}) .start();
		
		new Thread(() -> {
			try {
				threadLocal.set(Thread.currentThread().getName());
				System.out.println(Thread.currentThread().getName() + "," + threadLocal.get());
			} finally {
				//必须手动释放资源，避免内存溢出
				threadLocal.remove();
				System.out.println(Thread.currentThread().getName() + "," + threadLocal.get());
			}
		}) .start();
		
		System.out.println(Thread.currentThread().getName() + "," + threadLocal.get());
	}
}
